//
//  TSBugTapOnScrollViewController.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 15/11/12.
//  Copyright (c) 2015年 dvlproad. All rights reserved.
//

#import "TSBugTapOnScrollViewController.h"
#import <CQDemoKit/CJUIKitToastUtil.h>
#import <CQDemoKit/CQTSRipeTableView.h>
#import <CQDemoKit/CQTSButtonFactory.h>

#import "UIViewController+TSBug.h"

@interface TSBugTapOnScrollViewController () <UITextViewDelegate, UIGestureRecognizerDelegate> {
    
}
@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, assign, readonly) BOOL fixBug;    /**< 是否修复bug（默认否） */

@end

@implementation TSBugTapOnScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(@"修复View上的UIScrollView阻断了UITouch事件传递", nil);
    
    UIButton *bugStateButton = [CQTSButtonFactory bugButtonWithFixBugHandle:^{
        [self fixBug:NO];
    } reproduceBugHandle:^{
        [self fixBug:YES];
    }];
    [self.view addSubview:bugStateButton];
    [bugStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(10);
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    
    UILabel *knowledgeLabel = [self knowledgeLabel];
    knowledgeLabel.text = @"1、 UIScrollView覆盖在底层的UIView上面会接收UITouch的事件，事件就不会向下传递了，这时需要添加一个类别来让UISrollView不接收这个事件并把这个事件传递下去，";
    [self.view addSubview:knowledgeLabel];
    [knowledgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(bugStateButton.mas_bottom).mas_offset(10);
        //make.height.mas_equalTo(160);
    }];
    
    
    self.containerView.backgroundColor = UIColor.lightGrayColor;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankViewAction:)];
    tapGesture.delegate = self;
    [self.view addGestureRecognizer:tapGesture];
}



#pragma mark - Event
/*
 *  是否要修复bug
 *
 *  @param shouldFixBug 是否要修复bug（YES:修复bug; NO:重现bug）
 */
- (void)fixBug:(BOOL)shouldFixBug {
    _fixBug = shouldFixBug;
}


/** 点击空白区域的事件 */
- (void)tapBlankViewAction:(UITapGestureRecognizer *)tap {
    [self __showToast:@"响应了TapGR" centerOffset:CGPointMake(0, 100)];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
    
    [self __showToast:@"响应了TouchBegin" centerOffset:CGPointMake(0, 40)];
}


#pragma mark - UIGestureRecognizerDelegate
//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    return YES;
//}
///// 决定是否手势继续向下响应（找到cell的时候就不继续了）
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    if (self.fixBug == NO) {
//        return YES; // 不处理此方法的时候，其默认值为YES
//    }
//
//    UIView *touchView = touch.view;
////    if ([touchView isDescendantOfView:self.tableView]) {
////        CGPoint locationPoint = [touch locationInView:self.tableView];
////        NSIndexPath *clickIndexPath = [self.tableView indexPathForRowAtPoint:locationPoint];
////        //NSIndexPath *clickIndexPath = [self.collectionView indexPathForItemAtPoint:locationPoint];
////        if (clickIndexPath != nil) {
////            return NO;
////        }
////    }
//
//    if ([NSStringFromClass([touchView class]) isEqualToString:@"UIScrollView"]) {
//        return NO;
//    }
//
//    return YES;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
